// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.models;

import java.util.List;

import org.dengues.core.prefs.IDenguesPrefsConstant;
import org.dengues.core.process.CompElement;
import org.dengues.core.process.ICompBlockExit;
import org.dengues.core.process.ICompConnection;
import org.dengues.core.process.ICompProcess;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.ui.DenguesUiPlugin;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-16 qiang.zhang $
 * 
 */
public class CompBlockExit extends CompElement implements ICompBlockExit {

    private final String blockId;

    private ICompProcess compProcess;

    private Dimension size = new Dimension(0, 0);

    private Point location = new Point(0, 0);

    private String name;

    private RGB color;

    public static final String PROP_BLOCKEXIT_SIZE = "CompBlockExit_size"; //$NON-NLS-1$

    public static final String PROP_BLOCKEXIT_COLOR = "CompBlockExit_color"; //$NON-NLS-1$

    public static final String PROP_BLOCKEXIT_LOCATION = "CompBlockExit_location"; //$NON-NLS-1$

    public static final String PROP_BLOCKEXIT_NAME = "CompBlockExit_name"; //$NON-NLS-1$

    private boolean isEnter;

    public CompBlockExit(String uuid, String name) {
        if (uuid == null) {
            uuid = WarehouseResourceFactory.getEMFId();
        }
        this.blockId = uuid;
        this.name = name;
    }

    private static final long serialVersionUID = 164225645764574443L;

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.gef.models.Element#getElementName()
     */
    @Override
    public String getCompName() {
        return this.name;
    }

    /**
     * Getter for color.
     * 
     * @return the color
     */
    @Override
    public RGB getColor() {
        return this.color;
    }

    /**
     * Sets the color.
     * 
     * @param color the color to set
     */
    public void setColor(RGB color) {
        if (this.color == null
                || this.color.equals(PreferenceConverter.getColor(DenguesUiPlugin.getDefault().getPreferenceStore(),
                        IDenguesPrefsConstant.DESIGNER_COLOR_BLOCK))) {
            this.color = color;
            fireStructureChange(PROP_BLOCKEXIT_COLOR, this.color);
        }
    }

    /**
     * Getter for compProcess.
     * 
     * @return the compProcess
     */
    @Override
    public ICompProcess getCompProcess() {
        return this.compProcess;
    }

    /**
     * Sets the compProcess.
     * 
     * @param compProcess the compProcess to set
     */
    public void setCompProcess(CompProcess compProcess) {
        this.compProcess = compProcess;
    }

    /**
     * Getter for size.
     * 
     * @return the size
     */
    @Override
    public Dimension getSize() {
        return this.size;
    }

    /**
     * Sets the size.
     * 
     * @param size the size to set
     */
    @Override
    public void setSize(Dimension size) {
        this.size = size;
        fireStructureChange(PROP_BLOCKEXIT_SIZE, this.size);
    }

    /**
     * Getter for location.
     * 
     * @return the location
     */
    @Override
    public Point getLocation() {
        return this.location;
    }

    /**
     * Sets the location.
     * 
     * @param location the location to set
     */
    @Override
    public void setLocation(Point location) {
        this.location = location;
        List<ICompConnection> conns = getOutgoingConnections();
        for (ICompConnection compConnection : conns) {
            CompConnection compConnection2 = ((CompConnection) compConnection);
            compConnection2.getConnectionLabel().changeLocation();
            if (compConnection2.getConnectionTrac() != null) {
                compConnection2.getConnectionTrac().changeLocation();
            }
        }
        conns = getIncomingConnections();
        for (ICompConnection compConnection : conns) {
            CompConnection compConnection2 = ((CompConnection) compConnection);
            compConnection2.getConnectionLabel().changeLocation();
            if (compConnection2.getConnectionTrac() != null) {
                compConnection2.getConnectionTrac().changeLocation();
            }
        }
        fireStructureChange(PROP_BLOCKEXIT_LOCATION, this.location);
    }

    /**
     * Sets the name.
     * 
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
        fireStructureChange(PROP_BLOCKEXIT_NAME, this.name);
    }

    /**
     * Getter for blockId.
     * 
     * @return the blockId
     */
    public String getBlockId() {
        return this.blockId;
    }

    /**
     * Getter for isEnter.
     * 
     * @return the isEnter
     */
    public boolean isEnter() {
        return this.isEnter;
    }

    /**
     * Sets the isEnter.
     * 
     * @param isEnter the isEnter to set
     */
    public void setEnter(boolean isEnter) {
        this.isEnter = isEnter;
        if (isEnter) {
            this.color = Display.getDefault().getSystemColor(SWT.COLOR_DARK_GREEN).getRGB();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.CompElement#isOutput(java.lang.String)
     */
    @Override
    public boolean isOutput(String string) {
        return isEnter();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.CompElement#isInput(java.lang.String)
     */
    @Override
    public boolean isInput(String string) {
        return !isEnter();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompBlockExit#setCompProcess(org.dengues.core.process.ICompProcess)
     */
    public void setCompProcess(ICompProcess compProcess) {
        this.compProcess = compProcess;
    }

}
